package com.erp.itm.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.ShiroUtils;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.itm.domain.ProjecttypeDO;
import com.erp.itm.service.ProjecttypeService;
import com.erp.common.utils.PageUtils;
import com.erp.common.utils.Query;
import com.erp.common.utils.R;

/**
 * 项目类型
 * 
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-07-12 18:35:10
 */
 
@Controller
@RequestMapping("/itm/projecttype")
public class ProjecttypeController {
	@Autowired
	private ProjecttypeService projecttypeService;
	
	@GetMapping()
	@RequiresPermissions("itm:projecttype:projecttype")
	String Projecttype(){
	    return "itm/projecttype/projecttype";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("itm:projecttype:projecttype")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ProjecttypeDO> projecttypeList = projecttypeService.list(query);
		int total = projecttypeService.count(query);
		PageUtils pageUtils = new PageUtils(projecttypeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("itm:projecttype:add")
	String add(){
	    return "itm/projecttype/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("itm:projecttype:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ProjecttypeDO projecttype = projecttypeService.get(id);
		model.addAttribute("projecttype", projecttype);
	    return "itm/projecttype/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("itm:projecttype:add")
	public R save( ProjecttypeDO projecttype){
		UserDO current = ShiroUtils.getUser();
		projecttype.setUserIdCreate(current.getUserId());
		projecttype.setUserNameCreate(current.getName());
		projecttype.setGmtCreate(new Date());
		projecttype.setUserIdModified(current.getUserId());
		projecttype.setUserNameModified(current.getName());
		projecttype.setGmtModified(new Date());
		if(projecttypeService.save(projecttype)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("itm:projecttype:edit")
	public R update( ProjecttypeDO projecttype){
		UserDO current = ShiroUtils.getUser();
		projecttype.setUserIdModified(current.getUserId());
		projecttype.setUserNameModified(current.getName());
		projecttype.setGmtModified(new Date());
		projecttypeService.update(projecttype);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("itm:projecttype:remove")
	public R remove( Long id){
		if(projecttypeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("itm:projecttype:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		projecttypeService.batchRemove(ids);
		return R.ok();
	}
	
}
